﻿using System;
using System.Diagnostics;
using Smarly.Idml.RelaxNg.Generator.Model;
using Tenuto.Grammar;

namespace Smarly.Idml.RelaxNg.Generator.Proxy
{
	public class DataExpBuilder : Builder<DataExp>
	{
		[DebuggerStepThrough]
		protected internal DataExpBuilder(DataExp exp, Action<IObjectName> updateName) : base(exp, updateName) { }

		public override IStruct CreateIStruct()
		{
			//todo correct
			return null;
		}

		public override IStruct Fill(IStruct getStruct = null)
		{
			getStruct = base.Fill(getStruct);

			if (getStruct is AttrStruct)
			{
				Type type = ParseRelaxNgType(Exp.dt.GetType());

				//todo remove condition
				if (type == null)
					return getStruct;

				((AttrStruct) getStruct).Type = type.FullName;
			}
			else if (getStruct is ObjectStruct)
			{
				//todo implement
			}
			else
			{
				//todo remove condition
				if (getStruct == null)
					return getStruct;

				throw new NotImplementedException("Not implement for type - " + getStruct.GetType().FullName);
			}

			return getStruct;
		}

		protected Type ParseRelaxNgType(Type relaxNgType1)
		{
			Type result;
			if (relaxNgType1 == typeof(Tenuto.Datatype.XSDLib.StringType))
			{
				result = typeof(string);
			}
			else if (relaxNgType1 == typeof(Tenuto.Datatype.XSDLib.BooleanType))
			{
				result = typeof(bool);
			}
			else if (relaxNgType1 == typeof(Tenuto.Datatype.XSDLib.IntType))
			{
				result = typeof(int);
			}
			else if (relaxNgType1 == typeof(Tenuto.Datatype.XSDLib.DoubleType))
			{
				result = typeof(double);
			}
			else if (relaxNgType1 == typeof(Tenuto.Datatype.XSDLib.ShortType))
			{
				result = typeof(short);
			}
			else if (relaxNgType1 == typeof(Tenuto.Datatype.XSDLib.UnimplementedDatatype))
			{
				//todo remove return
				return null;
				throw new NotImplementedException(relaxNgType1.FullName);
			}
			else if (relaxNgType1 == typeof(Tenuto.Datatype.XSDLib.RangeFacet))
			{
				//todo remove return
				return null;
				throw new NotImplementedException(relaxNgType1.FullName);
			}
			else if (relaxNgType1 == typeof(Tenuto.Grammar.ListExp))
			{
				//todo remove return
				return null;
				throw new NotImplementedException(relaxNgType1.FullName);
			}
			else
			{
				throw new NotImplementedException(relaxNgType1.FullName);
			}

			return result;
		}

	}
}